package com.lipiao.makerandroid.View.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lipiao.makerandroid.Bean.RespondBean.UserInfoBean;
import com.lipiao.makerandroid.R;
import com.lipiao.makerandroid.Utils.HttpUtil;
import com.lipiao.makerandroid.Utils.LogUtil;
import com.lipiao.makerandroid.View.Activity.EditActivity;
import com.lipiao.makerandroid.View.Activity.IssueActivity;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class UserFragment extends Fragment {

    static String userNumber;//账号
    static String password;//密码

    UserInfoBean userInfoBean = new UserInfoBean();

    String TAG = "UserFragment";
    View rootView;
    LinearLayout imageViewMyinfo;
    LinearLayout imageViewMyissue;
    LinearLayout imageViewaboutus;

    public UserFragment() {

    }

    public static UserFragment newInstance(String username, String password) {
        UserFragment fragment = new UserFragment();
        Bundle bundle = new Bundle();
        bundle.putString("userNumber", username);
        bundle.putString("password", password);
        fragment.setArguments(bundle);
        return fragment;
    }

    //创建碎片视图
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.user_info, container, false);
        initView(rootView);
        assert getArguments() != null;//判空处理
        userNumber = getArguments().getString("userNumber");//获取账号
        System.out.println("获取userNumber为："+userNumber);
//        password = getArguments().getString("password");//获取
//        Log.d(TAG, "onCreateView: userNumber " + userNumber + " password " + password);

        imageViewMyinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                System.out.println("setOnClickListener"+userNumber);
                Call<ResponseBody> callInfo = HttpUtil.getUserService().getUserInfo(userNumber);
                callInfo.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            String strBack = response.body().string();
                            JSONObject jsonObject = new JSONObject(strBack);
                            Gson gson = new Gson();
                            userInfoBean = gson.fromJson(jsonObject.toString(),new TypeToken<UserInfoBean>(){}.getType());
                            Intent i = new Intent(getContext(), EditActivity.class);
                            i.putExtra("userNumber",userNumber);
                            i.putExtra("userName",userInfoBean.getUserName());
                            i.putExtra("userEmail",userInfoBean.getUserinfoEmail());
                            i.putExtra("userTel",userInfoBean.getUserinfoTel());
                            i.putExtra("userGender",userInfoBean.getUserinfoGender());
                            getActivity().startActivity(i);
                        } catch (IOException | JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {

                    }
                });
                Intent i = new Intent(getContext(), EditActivity.class);
                i.putExtra("userNumber",userNumber);

            }
        });
        imageViewMyissue.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), IssueActivity.class);
                intent.putExtra("userNumber",userNumber);
                getActivity().startActivity(intent);
            }
        });
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    //碎片是否可见 可见下重新加载数据
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.d(TAG, "setUserVisibleHint: " + isVisibleToUser);
        if (isVisibleToUser) {
            //用户可见 获取数据并将数据放到视图控件中
            initData();
            fetchData();
        }
    }

    //实例化控件
    public void initView(View view) {
        imageViewMyinfo = view.findViewById(R.id.myinfo);
        imageViewMyissue = view.findViewById(R.id.myissue);
        imageViewaboutus = view.findViewById(R.id.aboutus);
    }

    //获取数据
    protected void initData() throws NullPointerException {
        LogUtil.d(TAG, "initData");
    }

    //将数据放到视图控件中
    public void fetchData() {
    }

}
